package io.dshubs.archives.controller;


import io.dshubs.archives.entity.ArchiveWarehouseLocation;
import io.dshubs.archives.entity.ArchiveWarehouseZone;
import io.dshubs.archives.service.ArchiveWarehouseLocationService;
import io.dshubs.archives.service.ArchiveWarehouseZoneService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 密架货位表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-17
 */
@Api(tags = "密架货位表")
@RestController
@RequestMapping("/warehouse-location")
public class ArchiveWarehouseLocationController {

    @Autowired
    private ArchiveWarehouseLocationService archiveWarehouseLocationService;

    @GetMapping("/page")
    @ApiOperation("密架货位:page")
    @LogOperation("密架货位:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "warehouseId", value = "库房id", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "zoneId", value = "库区id", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "code", value = "货位编号", paramType = "query", dataType="String"),
    })
    public Result<PageData<ArchiveWarehouseLocation>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveWarehouseLocation>>().ok(archiveWarehouseLocationService.page(params));
    }


    @PostMapping
    @ApiOperation("保存")
    @LogOperation("密架货位:save")
    public Result save(@RequestBody ArchiveWarehouseLocation archiveWarehouseLocation){
        //效验数据
        ValidatorUtils.validateEntity(archiveWarehouseLocation, AddGroup.class, DefaultGroup.class);

        archiveWarehouseLocationService.save(archiveWarehouseLocation);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("密架货位:update")
    public Result update(@RequestBody ArchiveWarehouseLocation archiveWarehouseLocation){
        //效验数据
        ValidatorUtils.validateEntity(archiveWarehouseLocation, UpdateGroup.class, DefaultGroup.class);
        archiveWarehouseLocationService.updateById(archiveWarehouseLocation);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("密架货位:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveWarehouseLocationService.deleteBatchIds(ids);
        return new Result();
    }


    @GetMapping("/{id}")
    @ApiOperation("查看")
    @LogOperation("密架货位:get")
    public Result<ArchiveWarehouseLocation> get(@PathVariable Long id){
        return new Result<ArchiveWarehouseLocation>().ok( archiveWarehouseLocationService.selectById(id));
    }


}
